﻿using System;
using Buddy.BehaviorTree;
using Buddy.CommonBot;
using Buddy.Swtor;
using System.Linq;
using WingIt.Dynamics;

namespace WingIt.Routines
{
    public static class JediSentinel
    {
        [Class(CharacterClass.Knight, AdvancedClass.Sentinel, SkillTreeId.None)]
        [Behavior(BehaviorType.Combat)]
        public static Composite JediSentinelCombat()
        {
            return new PrioritySelector(
                // Todo: Handle centering effects, which seems like quite an important mechanic!
                Movement.StopInRange(0.35f),
                Spell.WaitForCast(),

                Spell.Cast("Resolute", castWhen => BuddyTor.Me.IsStunned),

                // Added by Willox 24.03.2012
                // Interrupt (Kicks the target and interrupts its current action, preventing that ability from being used for the next 4 seconds.)
                // Spell: Force Kick
                // Instant / Cooldown: 8s / Range: 4 m
                Spell.Cast("Force Kick", ret => BuddyTor.Me.CurrentTarget.IsCasting),

                // See if we need to CC anything before we do anything else.
                Spell.Cast("Awe", onUnit =>
                {
                    var previousTarget = BuddyTor.Me.CurrentTarget;
                    return
                        Helpers.Targets.FirstOrDefault(
                            t =>
                            t != previousTarget && (t.Toughness == CombatToughness.Strong)) ??
                        Helpers.Targets.FirstOrDefault(t => t != previousTarget);
                }, castWhen => Helpers.Targets.Count() >= 3),

                // Can't leap when in melee range. Its usable when out of melee. So use it whenever its available!
                Spell.Cast("Force Leap"),

                // Execute them if possible. This is always our top priority.
                Spell.Cast("Dispatch", castWhen => BuddyTor.Me.CurrentTarget.HealthPercent <= 20 && BuddyTor.Me.ResourceStat >= 3),

                // Our "shields"
                Spell.BuffSelf("Rebuke", castWhen => BuddyTor.Me.InCombat && BuddyTor.Me.HealthPercent <= 90),
                Spell.BuffSelf("Saber Ward", castWhen => BuddyTor.Me.InCombat && BuddyTor.Me.HealthPercent <= 70),

                // Stun the elite!
                Spell.Cast("Force Sweep", castWhen => BuddyTor.Me.CurrentTarget.Toughness == CombatToughness.Strong),

                // Interrupt them on the last possible moment.
                Spell.Cast("Force Kick", castWhen => BuddyTor.Me.CurrentTarget.IsCasting && BuddyTor.Me.CurrentTarget.CastTimeEnd - TimeSpan.FromSeconds(1) >= DateTime.Now),

                // Spells using focus.
                Spell.Cast("Slash"),
                Spell.Cast("Precision Slash"),

                // Our Focus building spells.
                Spell.Cast("Zealous Strike"),
                Spell.Cast("Strike"),

                Movement.MoveTo(ret => BuddyTor.Me.CurrentTarget.Position, 0.3f)
                );
        }

        //[Class(CharacterClass.Knight, AdvancedClass.Sentinel)]
        //[Behavior(BehaviorType.Pull)]
        //public static Composite KnightPull()
        //{
        //    return Spell.Cast("Force Leap", castWhen => BuddyTor.Me.CurrentTarget.Distance > BuddyTor.Me.MeleeDistance);
        //}
    }
}
